package user

import (
	"chaowan/data_source"
	"chaowan/models"
	"chaowan/service"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"strings"
	"time"
)

type UserinfoBody struct {
	Uid       int64  `json:"uid" gorm:"uid"`
	Nickname  string `json:"nickname" gorm:"nickname"`
	Phone     string `json:"phone" gorm:"phone"`
	Logo      string `json:"logo" gorm:"logo"`
	Phonecode string `json:"phonecode" gorm:"phonecode"`
	Cid       string `json:"cid" gorm:"cid"`
	Title     string `json:"title" gorm:"title"`
	Body      string `json:"body" gorm:"body"`
}

func Usercid(ctx *gin.Context) {
	request := UserinfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	cid := request.Cid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("cid", cid)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Tuisongcs(ctx *gin.Context) {
	request := UserinfoBody{}
	ctx.ShouldBind(&request)
	title := request.Title
	body := request.Body
	var user []models.User
	data_source.Db.Find(&user)
	var cids []string
	for _, u := range user {

		if u.Cid != "" {
			cids = append(cids, u.Cid)
		}

	}
	service.Getuixx(cids, title, body)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Userinfo(ctx *gin.Context) {
	request := UserinfoBody{}
	ctx.ShouldBind(&request)
	fmt.Println(request)
	uid := request.Uid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)

	var vip models.Vipgift
	data_source.Db.Where("dengji = ?", user.Vip).First(&vip)
	user.Viplogo = vip.Image

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    user,
		"msg":     "ok",
	})
	return
}

func Userdel(ctx *gin.Context) {
	request := UserinfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("status", 2)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type PhonecodeBody struct {
	Phone string `json:"phone" from:"phone"`
}

func Phonecode(ctx *gin.Context) {
	request := PhonecodeBody{}
	ctx.ShouldBind(&request)
	phone := request.Phone
	phone = strings.Replace(phone, " ", "", -1)
	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
	vcode := fmt.Sprintf("%06v", rnd.Int31n(1000000))

	par, _ := json.Marshal(map[string]interface{}{
		"code": vcode,
	})
	_, err := SendSms("LTAI5tQWfDLVJSP8Pom4Upwq", "0jBcWAfLoLVt54UL4PfLUPrGZ5dT1r", "海南许个愿网络科技", phone, "SMS_304065400", string(par))
	fmt.Println(err)
	if err == nil {
		phonecode := models.Phonecode{
			Phone:   phone,
			Code:    vcode,
			Enttime: time.Now().Unix() + 900,
		}
		data_source.Db.Create(&phonecode)
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     err,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Userupdate(ctx *gin.Context) {
	request := UserinfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	nickname := request.Nickname
	logo := request.Logo
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	user := models.User{
		Nickname: nickname,
		Logo:     logo,
	}
	data_source.Db.Where("id = ?", uid).Updates(&user)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Userliushui(ctx *gin.Context) {
	request := UserinfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	inttime, endtime := getCurrentdayStartAndEnd()

	var userdayprice []models.Userdayprice
	data_source.Db.Where("uid = ?", uid).Where("addtime >= ?", inttime).Where("addtime < ?", endtime).Find(&userdayprice)
	var price float64
	for _, v := range userdayprice {
		price = price + v.Price
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    price,
		"msg":     "ok",
	})
	return
}

// 今天开始结束时间戳
func getCurrentdayStartAndEnd() (startTime, endTime int64) {
	// 获取当前时间
	//今天开始
	//startOfToday := time.Now().Truncate(24 * time.Hour)
	startTimestamp := getTodayMidnightTimestamp()
	// 获取今天的结束时间
	//endOfToday := startOfToday.Add(24 * time.Hour).Add(-1 * time.Second)
	endTimestamp := startTimestamp + 86400

	return startTimestamp, endTimestamp
}
